package com.lty.code;

import cn.hutool.core.io.FileUtil;
import com.lty.code.bean.RequestEntity;
import com.lty.constant.BaseConstant;
import com.lty.util.BaseUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * DTO(request)代码生成
 * @author lty
 */
public class RequestGenerate {

    public static void main(String[] args) throws Exception {
        //创建资源加载器
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("/btl/");
        //创建默认配置
        Configuration cfg = Configuration.defaultConfiguration();
        //创建模板引擎
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);

        //生成代码(自行补充实体类字段)
        generateCode(gt);

        //删除代码(解除注释)
        //deleteCode();
    }

    /**
     * 类名(注意大写)
     */
    public static final String CLASS_NAME = MybatisGenerate.CLASS_NAME;

    /**
     * 作者
     */
    public static final String AUTHOR = MybatisGenerate.AUTHOR;

    /**
     * 对应包(文件自动生成至该包下)
     */
    private static final String RESULT_PACKAGE = "com.lty.model.dto." + MybatisGenerate.isFirstUpper(CLASS_NAME, false);

    /** 生成文件夹路径 */
    private static final String CODE_PATH = BaseConstant.PROJECT_ROOT_DIRECTORY + "/src/main/java/" + BaseUtil.dotToLine(RESULT_PACKAGE);


    private static void generateCode(GroupTemplate gt) throws Exception {
        //加载模板文件
        Template addRequestTemplate = gt.getTemplate("addRequest.btl");
        Template queryRequestTemplate = gt.getTemplate("queryRequest.btl");
        Template updateRequestTemplate = gt.getTemplate("updateRequest.btl");

        //生成xxxAddRequest
        bindRequest(addRequestTemplate, "AddRequestEntity");
        generateFile(addRequestTemplate, "AddRequest");

        //生成xxxQueryRequest
        bindRequest(queryRequestTemplate, "QueryRequestEntity");
        generateFile(queryRequestTemplate, "QueryRequest");

        //生成xxxUpdateRequest
        bindRequest(updateRequestTemplate, "UpdateRequestEntity");
        generateFile(updateRequestTemplate, "UpdateRequest");
    }

    /**
     * 绑定请求参数
     * @param template 模板对象
     * @param varName 模板内容绑定对象
     */
    private static void bindRequest(Template template, String varName) {
        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setClassName(CLASS_NAME);
        requestEntity.setResultPackage(RESULT_PACKAGE);
        requestEntity.setAuthor(AUTHOR);
        template.binding(varName, requestEntity);
        // 渲染输出内容
        String templateResult = template.render();
        System.out.println(templateResult);
    }

    /**
     * 生成文件方法
     * @param template  模板构造器
     * @param fileHeader 文件头
     */
    private static void generateFile(Template template, String fileHeader) throws IOException {
        OutputStream out = null;
        String fileUrl = CODE_PATH + "/" + CLASS_NAME + fileHeader + ".java";
        File newFile = new File(fileUrl);
        File newFileDir = newFile.getParentFile();
        //判断生成文件目录是否存在
        if (!newFileDir.exists()) {
            newFileDir.mkdirs();
        }
        //判断生成文件是否存在
        if (!newFile.exists()) {
            newFile.createNewFile();
            out = new FileOutputStream(newFile);
            template.renderTo(out);
            System.out.println("生成文件成功");
        } else {
            // 若文件存在则不重新生成
            System.out.println("文件已存在");
        }
    }

    /**
     * 删除生成代码文件夹
     * @author lty
     */
    public static void deleteCode() throws Exception {
        File generateFile = new File(CODE_PATH);
        System.out.println(CODE_PATH);
        if (generateFile.exists()) {
            FileUtil.clean(generateFile);
            System.out.println("删除成功");
        }
    }

}
